home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2289 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  43 lines

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: 16 Jan 1996 17:50:58 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dgoi2$ij8@colossus.holonet.net>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Holger Maier (100336.3326@CompuServe.COM) wrote:
  13. : Consider
  14. : #include <iostream>
  15. : int main() {
  16. :   int i=1;int j=i+(i+=1);
  17.                   ^^^^^^^^
  18. This expression uses i and also assigns to it, without any
  19. intervening sequence point.  Its value is therefore undefined.
  20.  
  21. :   cout<<i<<','<<j<<'\n';
  22. :   return 0;
  23. : }
  24. : on my compiler this produces 2,4
  25. : Looked up the ARM:
  26. : 5: .. The order of evaluation of subexpressions is determined by the
  27. : precedence and grouping of operators.
  28. ...
  29. Order of evaluation does not mean order in time; instead, it's
  30. a logical ordering.  In your statement, the compiler is free
  31. to evaluate i first, then i+=1, and then the sum; or it can
  32. just as easily reverse the first two operations in time.
  33.  
  34. Your compiler is conforming to the standard -- i.e. undefined
  35. behavior.
  36.  
  37. : BTW: I know we should not code like that...
  38.  
  39. Yes, in fact we have to be diligent it doesn't happen by accident
  40. in some more complicated expression.
  41. --
  42. Russell Blackadar,   russell@mdli.com
  43.